package com.wxz.gblw.cjg.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxz.gblw.cjg.common.model.BeanOperation;
import com.wxz.gblw.cjg.common.model.RequestFilters;
import com.wxz.gblw.cjg.common.model.Tree;
import com.wxz.gblw.cjg.common.base.BaseWxzService;
import com.wxz.gblw.cjg.entity.KnowledgeType;
import com.wxz.gblw.cjg.mapper.KnowledgeTypeMapper;
import com.wxz.gblw.cjg.vo.KnowledgeTypeVo;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* KnowledgeType
* @author xiangzhong.wang
* @date 2019-12-31 15:17:33
*/
@Service
public class KnowledgeTypeService extends BaseWxzService<KnowledgeTypeMapper, KnowledgeType> {

    public boolean saveEntity(KnowledgeTypeVo voEntity) {
        KnowledgeType poEntity = new KnowledgeType();
        BeanCopier copier = BeanCopier.create(KnowledgeTypeVo.class, KnowledgeType.class,
                false);

        poEntity.setCreateTime(new Date());

        copier.copy(voEntity, poEntity, null);
        return save(poEntity);
    }

    public Object getDetails(Long id) {
        return this.getById(id);
    }

    public boolean editEntity(KnowledgeTypeVo voEntity) {
        KnowledgeType poEntity = getById(voEntity.getId());
        if(null == poEntity){
            //throw new ServiceLayerException(10001222,"计划类型或已被其他用户删除，请刷新后重试！！！");
        }

//        Map<String,Object> columnMap = new HashMap<>();
//        columnMap.put("year",voEntity.getYear());
//        columnMap.put("title",voEntity.getTitle());
//        List<PlanType> otList = (List<PlanType>) this.listByMap(columnMap);
//        if(otList!=null && !otList.isEmpty() && otList.get(0).getId() != voEntity.getId()){
//            throw new ServiceLayerException(10001222,"该年度计划类型标题已存在，请修改！！！");
//        }

        BeanCopier copier = BeanCopier.create(KnowledgeTypeVo.class, KnowledgeType.class,
                false);
        copier.copy(voEntity, poEntity, null);
        return this.saveOrUpdate(poEntity);
    }

    public boolean deleteEntity(Long id) {
        return removeById(id);
    }

    public Object queryEntitiesByPage(List<RequestFilters> params, long currentPage, long pageSize) throws Exception {
        /** 实例化查询构造器*/
        QueryWrapper<KnowledgeType> wrapper = new QueryWrapper<>();
        /** 选择查询列*/
        wrapper.select("id,name,view_order as viewOrder,code,create_time AS createTime,remark").orderByAsc("view_order");
        /** 构建查询sql*/
        BeanOperation.comboQueryConditions(wrapper,params);
        IPage<Map<String,Object>> pageMaps = pageMaps(new Page<>(currentPage, pageSize),wrapper);
        return pageMaps;
    }

    public Object tree() {
        QueryWrapper<KnowledgeType> wrapper = new QueryWrapper<KnowledgeType>();
        wrapper.orderByAsc("view_order");
        List<KnowledgeType> datas = this.list(wrapper);
        return this.getParentTree(datas);
    }

    /**
     * 获取父节点
     *
     * @param datas
     * @return
     */
    private List<Tree> getParentTree(List<KnowledgeType> datas) {
        List<Tree> parentTrees = new ArrayList<Tree>();
        datas.forEach(item -> {
            if (null == item.getParentKnowledgeTypeId() || item.getParentKnowledgeTypeId() == 0L) {
                Tree parent = new Tree();
                parent.setId(item.getId());
                parent.setName(item.getName());
                getChildrenTree(parent, datas);
                parentTrees.add(parent);
            }
        });
        return parentTrees;
    }

    /**
     * 获取子节点
     *
     * @param parent 父节点
     * @param datas
     */
    private void getChildrenTree(Tree parent, List<KnowledgeType> datas) {
        datas.forEach(item -> {
            if (null != item.getParentKnowledgeTypeId() && item.getParentKnowledgeTypeId() != 0L && item.getParentKnowledgeTypeId() == parent.getId()) {
                Tree child = new Tree();
                child.setId(item.getId());
                child.setParentNode(parent.getId());
                child.setName(item.getName());
                parent.getChildren().add(child);
                getChildrenTree(child, datas);
            }
        });
    }
}
